package com.mq.service.impl;

import com.mq.Mapper.FlightMapper;
import com.mq.Mapper.OrderMapper;
import com.mq.Mapper.TicketMapper;
import com.mq.pojo.Tickets;
import com.mq.pojo.User;
import com.mq.service.OrderService;
import com.mq.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    TicketMapper ticketMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    FlightMapper flightMapper;
    @Override
    public String LockOrder(User user, Integer flightId) {
        int NumTicket=flightMapper.cheakNumTicket(flightId);

        if (NumTicket==0)
            return "没有票了";
        else {
            flightMapper.setSeat(flightId,NumTicket-1);
            Double price=flightMapper.flightPrice(flightId);
            orderMapper.createOrder(user.getUserId(), flightId, price);
            return "锁单成功";
        }
    }


    @Override
    public String CancelLockOrder(User user, Integer OrderId) {


        orderMapper.CancelLockOrder(user.getUserId(),OrderId);
        Integer flightId= orderMapper.getFLightsByFlightIdInOrders(OrderId);
        int NumTicket=flightMapper.cheakNumTicket(flightId);
        flightMapper.setSeat(flightId,NumTicket-1);
        return "取消锁单";
    }


    @Override
    public Tickets PayOrder(User user, Integer OrderId, PaymentService payment) {
        Double price =orderMapper.getPriceByOrderId(OrderId);
        payment.pay(price,OrderId);
        return ticketMapper.createTicket(OrderId,user.getUsername(),user.getUserId(),price);
    }

    @Override
    public List<Map<String, Object>> getOrderList(User user) {
        return orderMapper.getOrderList(user.getUserId());
    }
}
